package bme.andraskindler.treenventory.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import bme.andraskindler.treenventory.R;
import bme.andraskindler.treenventory.TreenventoryApplication;
import bme.andraskindler.treenventory.fragment.InfoDialogFragment;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

/**
 * 
 * @author andraskindler
 * 
 * 
 *         Az alkalmazás nyitóképernyőjét jeleníti meg. Ez négy virtuális gombot jelent, mindegyik a megfelelő onClickListenerrel felvértezve. Ezek mellett egy menüt is inflatelek (beállítások, infó), az alsó sávban pedig a Bluetooth-kapcsolat állapotát mutatja.
 */
public class MainActivity extends SherlockFragmentActivity {
	private RelativeLayout mLayout;
	private Button mButtonCamera, mButtonBluetooth, mButtonList, mButtonMap;
	private TextView mFooter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
		setContentView(mLayout);

		mFooter = (TextView) mLayout.findViewById(R.id.footer_tv_title);
		mFooter.setText(TreenventoryApplication.getInstance().getConnectedDeviceName());

		mButtonCamera = (Button) mLayout.findViewById(R.id.main_btn_camera);
		mButtonCamera.setOnClickListener(mainOnClickListener);

		mButtonBluetooth = (Button) mLayout.findViewById(R.id.main_btn_settings);
		mButtonBluetooth.setOnClickListener(mainOnClickListener);

		mButtonMap = (Button) mLayout.findViewById(R.id.main_btn_sync);
		mButtonMap.setOnClickListener(mainOnClickListener);

		mButtonList = (Button) mLayout.findViewById(R.id.main_btn_list);
		mButtonList.setOnClickListener(mainOnClickListener);
	}

	private OnClickListener mainOnClickListener = new OnClickListener() {

		public void onClick(View v) {
			int id = v.getId();
			switch (id) {
			case R.id.main_btn_camera:
				startActivity(new Intent(TreenventoryApplication.getInstance(), CameraActivity.class));
				break;
			case R.id.main_btn_settings:
				startActivity(new Intent(TreenventoryApplication.getInstance(), BluetoothActivity.class));
				break;
			case R.id.main_btn_list:
				startActivity(new Intent(TreenventoryApplication.getInstance(), DetailsActivity.class));
				break;
			case R.id.main_btn_sync:
				startActivity(new Intent(TreenventoryApplication.getInstance(), SynchronizeActivity.class));
				break;
			default:
				break;
			}

		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		mFooter.setText(TreenventoryApplication.getInstance().getConnectedDeviceName());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_options:
			startActivity(new Intent(this, OptionsActivity.class));
			break;
		case R.id.menu_about:
			InfoDialogFragment.newInstance("Diplomamunka", "Készítette: Kindler András").show(getSupportFragmentManager(), InfoDialogFragment.class.getCanonicalName());
			break;
		default:
			break;
		}
		return true;
	}

}